﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Texfina.Core.Common;  
using Texfina.Core.Web;
using Texfina.Common.Mg;
using Texfina.Core.Data;  
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBORpt = Texfina.BORpt.Rh;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using CrystalDecisions.Shared;

namespace RH
{
    public partial class wfProcPlaBonBnd : SearchPage 
   {

       public const int _GRDLIST_STANULADO = 9;
       public const int _GRDLIST_STPROCESO = 10;
    
      protected void Page_Load(object sender, EventArgs e)
      {
                       
         this.LoadInitialSettings();
         if(this.IsPostBack){
            this.LoadInPostBack();
            return;
         } 

         this.LoadData();

      }

      public override void LoadInitialSettings()
      {
         Label lbl = (Label) this.Master.FindControl("lblTitle");
         lbl.Text = "Registro de Bonificaciones";
         this.Page.Form.DefaultButton = bttSearch.UniqueID;
      }

      public override void LoadListControls()
      {
          ETablaGenDet objEDet = new ETablaGenDet();
          MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

          objEDet.IdTabla = ConstMultitabla.MES;
          DataTable dt = objBOI.GetDetailByList(objEDet);
          WebHelper.LoadListControl(ddlidMes, dt, "dsDescrip", "idCodigo", true, true);

          objEDet.IdTabla = ConstMultitabla.FORMA_PAGO;
          dt = objBOI.GetDetailByList(objEDet);
          WebHelper.LoadListControl(ddlidForPago, dt, "dsDescrip", "idCodigo", true, true);

      }

      protected void grdBttCmdPrevPostBackDataBinding(Object sender, EventArgs e)
      {
          ImageButton ctl = (ImageButton)sender;
          ScriptManager sm = ScriptManager.GetCurrent(this.Page);
          sm.RegisterPostBackControl(ctl);
      }

      public override void SearchData()
      {
          EProcPlaniBandeja objE = new EProcPlaniBandeja();

          objE.IdPeriodo= Session[ConstSessionVar.PERIODO].ToString();
          objE.IdEmpresa= Session[ConstSessionVar.EMPRESA].ToString();

          if (this.ddlidMes.SelectedValue.ToString() == "")
              objE.IdMes = null;
          else
              objE.IdMes = this.ddlidMes.SelectedValue.ToString();

          if (this.ddlidForPago.SelectedValue.ToString() == "")
              objE.IdForPago = null;
          else
              objE.IdForPago = this.ddlidForPago.SelectedValue.ToString();

          if (this.txtidPlanilla.Text == "")
              objE.IdPlanilla = null;
          else
              objE.IdPlanilla = this.txtidPlanilla.Text;

          if (this.txtdsPlanilla.Text == "")
              objE.DsPlanilla = null;
          else
              objE.DsPlanilla = this.txtdsPlanilla.Text;

          IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.ProcPlani));
          DataTable dt = objBO.GetByCriteria(objE);

          this.GridSetDataBinding(grdList, dt);

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
          String strQS= "";

         switch(e.Item.Value){
            case ConstMenuToolbarValue.BUTTON_NEW:
               strQS = "wfProcPlaBonMnt.aspx?formEA=" + Convert.ToInt32(FormEditAction.AddNew).ToString();
               break; 
         }
         Response.Redirect(strQS);
      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      { 
         DataRow dr = this.GridGetRowFromDataKey(grdList, Convert.ToInt32(e.CommandArgument));
         FormEditAction formEA= FormEditAction.AddNew;
         this.EditRowIndex = Convert.ToInt32(e.CommandArgument);
         switch(e.CommandName)
         {
             case ConstGridRowCommand.GRIDEDIT:
               formEA = FormEditAction.Edit;
               break;
             case ConstGridRowCommand.GRIDDELETE:
               formEA = FormEditAction.Delete;
               break;
             case ConstGridRowCommand.GRIDPREVIEW:
               PrintReport(ConstGridRowCommand.GRIDPREVIEW);
               break;
         }
         String strQS = "wfProcPlaBonMnt.aspx?formEA=" + Convert.ToInt32(formEA).ToString()
              + "&IdMes=" + dr["idMes"].ToString() + "&IdForPago=" + dr["idForPago"].ToString()
              + "&IdPlanilla=" + dr["idPlanilla"].ToString() + "&NuProcAnual=" + dr["nuProcAnual"].ToString()
              + "&StProceso=" + dr["stProceso"].ToString();
         Response.Redirect(strQS);
      
      }
    
        protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {

              String strRI = e.Row.RowIndex.ToString();

              ImageButton bttedit = (ImageButton)e.Row.FindControl("grdBtt" + ConstGridRowCommand.GRIDEDIT);

              if (e.Row.Cells[_GRDLIST_STANULADO].Text == ConstFlagEstado.ACTIVADO)
              {
                  e.Row.ForeColor = System.Drawing.Color.Red;
              }
              else
              {
                  if (e.Row.Cells[_GRDLIST_STPROCESO].Text == ConstEstadoProcePlani.CERRADA)
                  {
                      e.Row.ForeColor = System.Drawing.Color.Blue;
                  }
              }
          }
      }

      protected void bttOk_Click(object sender, EventArgs e)
      {
          this.SearchData();
      }
      public override void PrintReport(string key)
      {
          DataSet ds = (DataSet)this.GetDataReport(key);
          if (ds.Tables[0].Rows.Count > 0)
          {
              ReportDocument objRpt = new ReportDocument();
              objRpt.Load(this.MapPath(ConstSistema.ROOT_FOLDER_PATH + "/Boni-Regu.rpt"));
              objRpt.SetDataSource(ds.Tables[0]);

              MemoryStream ms = new MemoryStream();

              Response.Clear();

              ms = (MemoryStream)objRpt.ExportToStream(ExportFormatType.PortableDocFormat);
              objRpt.Dispose();
              objRpt = null;

              Response.AddHeader("Content-Disposition", "attachment;filename=Bonificaciones.pdf");

              Response.Buffer = true;
              Response.ContentType = "application/pdf";

              ms.WriteTo(Response.OutputStream);
              Response.Flush();
              Response.End();
          }

      }

      public override object GetDataReport(string key)
      {
          EProcPlaReg objE = new EProcPlaReg();
          RHBORpt.IPersonal objBODet = (RHBORpt.IPersonal)WCFHelper.GetObject<RHBORpt.IPersonal>(typeof(RHBORpt.Personal));

          DataRow dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);

          objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
          objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          objE.IdForPago = dr["idForPago"].ToString();
          objE.IdPlanilla = dr["idPlanilla"].ToString();
          objE.NuProcAnual = int.Parse(dr["nuProcAnual"].ToString());
          objE.IdTipRegula = ConstTipoRegularizacion.BONIFICACION;

          DataTable dt = objBODet.GetByCriteriaProcPlaReg(objE);
          DataSet ds = new DataSet();
          ds.Tables.Add(dt);

          return ds;

      }
   }
}